package communication.common.business.util;


import communication.common.business.mail.EmailUserConfig;
import communication.common.business.mail.MailUtils;
import communication.common.business.mail.MonitorMailConfig;
import communication.common.business.mail.SimpleMail;
import io.vertx.core.MultiMap;
import io.vertx.core.Vertx;
import io.vertx.core.http.impl.headers.HeadersMultiMap;
import io.vertx.ext.web.client.WebClient;
import io.vertx.ext.web.client.WebClientOptions;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.springframework.beans.factory.annotation.Autowired;

import javax.mail.MessagingException;
import java.io.IOException;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicReference;


/**
 * @author zxl
 */
@Slf4j
@NoArgsConstructor
@Data
public class WebClientUtil {



    /**
     * 查询Ip地址的网站
     */
    private static  String WEBSITE="https://2022.ip138.com/";

    @Autowired
    private Vertx vertx;

    /**
     * 发起http请求的client
     */
    private WebClient client;

    /**
     * 公网ip地址
     */
    private AtomicReference<String> ipAddress = new AtomicReference<>();


    /**
     * 创建client , 由于只会在收到特定指令后才会触发创建，所以此处不考虑并发问题
     */
    public void initClient(){
        if(vertx == null){
            throw new RuntimeException("vertx is null ,can not create WebClient instance!");
        }
        WebClientOptions options = new WebClientOptions()
                .setUserAgent("My-App/1.2.3");
        options.setKeepAlive(false);
        client = WebClient.create(vertx,options);
    }


    /**
     * 获取本地主机在外网的Ip地址
     *  todo:
     *  实现逻辑： 请求公网上一个特定的查询ip地址的网站，从该网站获取ip地址
     *
     *
     * @return
     */
    public  void getInternetIpAddress(){

        if(client == null){
           initClient();
        }

        MultiMap multiMap = new HeadersMultiMap();
        multiMap.add("User-Agent","Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:96.0) Gecko/20100101 Firefox/96.0");
        multiMap.add("Accept","text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8");
        multiMap.add("Accept-Language","zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2");
//        multiMap.add("Accept-Encoding","gzip, deflate, br");
        multiMap.add("Referer","https://www.ip138.com/");
        multiMap.add("Connection","keep-alive");



        // 1. 请求 WEBSITE_PRE
        client.getAbs(WEBSITE)
                .putHeaders(multiMap)
                .ssl(true)
                .send()
                .onSuccess(resp->{
                    ipAddress.set(getIpAddressFromHtml(resp.bodyAsString("UTF8")));
                })
                .onFailure(System.out::println);

    }

    /**
     * 解析html页面数据，获取ip地址
     * @return
     */
    private static String getIpAddressFromHtml(String html){
        Document document = Jsoup.parse(html);
        String title = Objects.requireNonNull(document.getElementsByTag("title").first()).text();
        return title.split("您的IP地址是：")[1].trim();
    }


    /**
     *  todo:
     * 监控邮件
     */
    private void monitorMail(){

    }

}
